{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 5.16 \u589e\u52a0\u6216\u6539\u53d8\u5df2\u6253\u5f00\u6587\u4ef6\u7684\u7f16\u7801\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u5728\u4e0d\u5173\u95ed\u4e00\u4e2a\u5df2\u6253\u5f00\u7684\u6587\u4ef6\u524d\u63d0\u4e0b\u589e\u52a0\u6216\u6539\u53d8\u5b83\u7684Unicode\u7f16\u7801\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u60f3\u7ed9\u4e00\u4e2a\u4ee5\u4e8c\u8fdb\u5236\u6a21\u5f0f\u6253\u5f00\u7684\u6587\u4ef6\u6dfb\u52a0Unicode\u7f16\u7801/\u89e3\u7801\u65b9\u5f0f\uff0c\n\u53ef\u4ee5\u4f7f\u7528 io.TextIOWrapper() \u5bf9\u8c61\u5305\u88c5\u5b83\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import urllib.request\nimport io\n\nu = urllib.request.urlopen('http://www.python.org')\nf = io.TextIOWrapper(u, encoding='utf-8')\ntext = f.read()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u60f3\u4fee\u6539\u4e00\u4e2a\u5df2\u7ecf\u6253\u5f00\u7684\u6587\u672c\u6a21\u5f0f\u7684\u6587\u4ef6\u7684\u7f16\u7801\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5148\u4f7f\u7528 detach() \u65b9\u6cd5\u79fb\u9664\u6389\u5df2\u5b58\u5728\u7684\u6587\u672c\u7f16\u7801\u5c42\uff0c\n\u5e76\u4f7f\u7528\u65b0\u7684\u7f16\u7801\u65b9\u5f0f\u4ee3\u66ff\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u5728 sys.stdout \u4e0a\u4fee\u6539\u7f16\u7801\u65b9\u5f0f\u7684\u4f8b\u5b50\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import sys\nsys.stdout.encoding"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "sys.stdout = io.TextIOWrapper(sys.stdout.detach(), encoding='latin-1')\nsys.stdout.encoding"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u6837\u505a\u53ef\u80fd\u4f1a\u4e2d\u65ad\u4f60\u7684\u7ec8\u7aef\uff0c\u8fd9\u91cc\u4ec5\u4ec5\u662f\u4e3a\u4e86\u6f14\u793a\u800c\u5df2\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "I/O\u7cfb\u7edf\u7531\u4e00\u7cfb\u5217\u7684\u5c42\u6b21\u6784\u5efa\u800c\u6210\u3002\u4f60\u53ef\u4ee5\u8bd5\u7740\u8fd0\u884c\u4e0b\u9762\u8fd9\u4e2a\u64cd\u4f5c\u4e00\u4e2a\u6587\u672c\u6587\u4ef6\u7684\u4f8b\u5b50\u6765\u67e5\u770b\u8fd9\u79cd\u5c42\u6b21\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "f = open('sample.txt','w')\nf"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "f.buffer"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "f.buffer.raw"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cio.TextIOWrapper \u662f\u4e00\u4e2a\u7f16\u7801\u548c\u89e3\u7801Unicode\u7684\u6587\u672c\u5904\u7406\u5c42\uff0c\nio.BufferedWriter \u662f\u4e00\u4e2a\u5904\u7406\u4e8c\u8fdb\u5236\u6570\u636e\u7684\u5e26\u7f13\u51b2\u7684I/O\u5c42\uff0c\nio.FileIO \u662f\u4e00\u4e2a\u8868\u793a\u64cd\u4f5c\u7cfb\u7edf\u5e95\u5c42\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u539f\u59cb\u6587\u4ef6\u3002\n\u589e\u52a0\u6216\u6539\u53d8\u6587\u672c\u7f16\u7801\u4f1a\u6d89\u53ca\u589e\u52a0\u6216\u6539\u53d8\u6700\u4e0a\u9762\u7684 io.TextIOWrapper \u5c42\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e00\u822c\u6765\u8bb2\uff0c\u50cf\u4e0a\u9762\u4f8b\u5b50\u8fd9\u6837\u901a\u8fc7\u8bbf\u95ee\u5c5e\u6027\u503c\u6765\u76f4\u63a5\u64cd\u4f5c\u4e0d\u540c\u7684\u5c42\u662f\u5f88\u4e0d\u5b89\u5168\u7684\u3002\n\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u8bd5\u7740\u4f7f\u7528\u4e0b\u9762\u8fd9\u6837\u7684\u6280\u672f\u6539\u53d8\u7f16\u7801\u770b\u770b\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "f"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "f = io.TextIOWrapper(f.buffer, encoding='latin-1')\nf"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "f.write('Hello')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u7ed3\u679c\u51fa\u9519\u4e86\uff0c\u56e0\u4e3af\u7684\u539f\u59cb\u503c\u5df2\u7ecf\u88ab\u7834\u574f\u4e86\u5e76\u5173\u95ed\u4e86\u5e95\u5c42\u7684\u6587\u4ef6\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "detach() \u65b9\u6cd5\u4f1a\u65ad\u5f00\u6587\u4ef6\u7684\u6700\u9876\u5c42\u5e76\u8fd4\u56de\u7b2c\u4e8c\u5c42\uff0c\u4e4b\u540e\u6700\u9876\u5c42\u5c31\u6ca1\u4ec0\u4e48\u7528\u4e86\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "f = open('sample.txt', 'w')\nf"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "b = f.detach()\nb"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "f.write('hello')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e00\u65e6\u65ad\u5f00\u6700\u9876\u5c42\u540e\uff0c\u4f60\u5c31\u53ef\u4ee5\u7ed9\u8fd4\u56de\u7ed3\u679c\u6dfb\u52a0\u4e00\u4e2a\u65b0\u7684\u6700\u9876\u5c42\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "f = io.TextIOWrapper(b, encoding='latin-1')\nf"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5c3d\u7ba1\u5df2\u7ecf\u5411\u4f60\u6f14\u793a\u4e86\u6539\u53d8\u7f16\u7801\u7684\u65b9\u6cd5\uff0c\n\u4f46\u662f\u4f60\u8fd8\u53ef\u4ee5\u5229\u7528\u8fd9\u79cd\u6280\u672f\u6765\u6539\u53d8\u6587\u4ef6\u884c\u5904\u7406\u3001\u9519\u8bef\u673a\u5236\u4ee5\u53ca\u6587\u4ef6\u5904\u7406\u7684\u5176\u4ed6\u65b9\u9762\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "sys.stdout = io.TextIOWrapper(sys.stdout.detach(), encoding='ascii',\n                            errors='xmlcharrefreplace')\nprint('Jalape\\u00f1o')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6ce8\u610f\u4e0b\u6700\u540e\u8f93\u51fa\u4e2d\u7684\u975eASCII\u5b57\u7b26 \u00f1 \u662f\u5982\u4f55\u88ab &#241; \u53d6\u4ee3\u7684\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}